<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *订单管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'发票列表'),
	array('op'=>'listday','title'=>'日开票统计'),
	array('op'=>'listmonth','title'=>'月开票统计'),
);
$return['status']=$this->status;
$return['type']=$this->type;
$return['usertype']=$this->usertype;
$return['sendtype']=$this->sendtype;
if($_RQ['op']=='list'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['logsn']){
		$params['logsn like']='%'.trim($_RQ['logsn']).'%';
		$return['params']['logsn']=$_RQ['logsn'];
	}
	if($_RQ['piao_number']){
		$params['piao_number like']='%'.trim($_RQ['piao_number']).'%';
		$return['params']['piao_number']=$_RQ['piao_number'];
	}
	if($_RQ['to_name']){
		$params['to_name like']='%'.trim($_RQ['to_name']).'%';
		$return['params']['to_name']=$_RQ['to_name'];
	}
	if(isset($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=intval($_RQ['mid']);
	}
	if(isset($_RQ['status'])){
		$params['status']=$_RQ['status'];
		$return['params']['status']=intval($_RQ['status']);
	}
	if(isset($_RQ['type'])){
		$params['type']=$_RQ['type'];
		$return['params']['type']=intval($_RQ['type']);
	}
	if(isset($_RQ['usertype'])){
		$params['usertype']=$_RQ['usertype'];
		$return['params']['usertype']=intval($_RQ['usertype']);
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=$_RQ['sendtype'];
		$return['params']['sendtype']=intval($_RQ['sendtype']);
	}
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['psize']){
		$_SESSION['psize']=$_RQ['psize'];
	}
	$psize = $_SESSION['psize']?:10;
	$list=pdo_getslice('jy_aihire_fapiao',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as $k=>&$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['address']=json_decode($item['address'],true);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'logsn','title'=>'记录编号','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'piao_number','title'=>'票据号码','type'=>'string'),
			array('name'=>'piao_fee','title'=>'金额','type'=>'string'),
			array('name'=>'piao_type','title'=>'消费类目','type'=>'string'),
			array('name'=>'to_name','title'=>'抬头','type'=>'string'),
			array('name'=>'usertype','title'=>'抬头类型','type'=>'radio','options'=>'usertype'),
			array('name'=>'type','title'=>'发票类型','type'=>'radio','options'=>'type'),
			array('name'=>'sendtype','title'=>'领票方式','type'=>'radio','options'=>'sendtype'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','op'=>'open','title'=>'开票','show'=>array('status'=>0)),
			array('type'=>'modal','op'=>'fa','title'=>'发快递','show'=>array('status'=>3,'sendtype'=>1)),
            array('type'=>'modal','op'=>'look','title'=>'开票详情'),
		)),
		'search'=>array(
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
			array('name'=>'logsn','width'=>10,'title'=>'记录编号','type'=>'string'),
			array('name'=>'piao_number','width'=>10,'title'=>'票据号码','type'=>'string'),
			array('name'=>'to_name','width'=>10,'title'=>'抬头','type'=>'string'),
			array('name'=>'usertype','width'=>10,'title'=>'抬头类型','type'=>'radio','options'=>'usertype'),
			array('name'=>'type','width'=>10,'title'=>'发票类型','type'=>'radio','options'=>'type'),
			array('name'=>'sendtype','width'=>10,'title'=>'领票方式','type'=>'radio','options'=>'sendtype'),
			array('name'=>'status','width'=>10,'title'=>'状态','type'=>'radio','options'=>'status'),
		),
	);
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='open'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$post['images']){
            exi('请上传发票PDF！','error');
		}
        $status=1;
		$update=array('upfile'=>$post['upfile'],'status'=>$status);
		pdo_update('jy_aihire_fapiao',$update,array('id'=>$id));
		exi('上传发票成功！','','//list/');
	}
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('list',array('id'=>$id));
	$item['images']=json_decode($item['images'],true);
	$htmls=array(
		'type'=>'edit',
		'desc'=>'上传发票',
	);
	$htmls['edit']=array(
		array('type'=>'string','title'=>'SN','name'=>'logsn','read'=>true),
		array('type'=>'upfile','title'=>'发票PDF','name'=>'pdf'),
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='fa'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if(!$post['express'] || !$post['expresssn']){
			exi('请填写快递信息','error');
		}
		$data=array('express'=>$post['express'],'expresssn'=>$post['expresssn']);
		$this->model('model')->piao_send($id,$data);
		exi('已发货！','','/fapiao/');
	}
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('fapiao',array('id'=>$id));	
	$item['express_type']=0;
	$tip='发票已打印，上传,快递发货！';
    $htmls=array(
        'type'=>'edit',
        'desc'=>'发货',
    );
	$htmls['edit']=array(
			array('type'=>'tip','title'=>$tip),
			array('type'=>'select','title'=>'快递公司','name'=>'express','options'=>'express','option_title'=>'name'),
			array('type'=>'string','title'=>'快递单号','name'=>'expresssn'),
	);
	$return['express']=model('jy_weishop/model')->express_types();
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='look'){
    $id=intval($_RQ['id']);
    $item=$this->pdo_get('fapiao',array('id'=>$id));
    $item['member']=cfc('core')->member_show($item['mid']);
    $item['address']=json_decode($item['address'],true);
    $item['addressinfo']=model('core/member')->get_address_str($item['address']);
    $htmls=array(
        'type'=>'look',
        'desc'=>'详情',
    );
    $htmls['look']=array(
        array('name'=>'logsn','title'=>'记录编号','type'=>'string'),
        array('name'=>'member','title'=>'会员信息','type'=>'member'),
        array('name'=>'piao_fee','title'=>'金额','type'=>'string'),
        array('name'=>'piao_type','title'=>'消费类目','type'=>'string'),
        array('name'=>'to_name','title'=>'抬头','type'=>'string'),
        array('name'=>'usertype','title'=>'抬头类型','type'=>'radio','options'=>'usertype'),
        array('name'=>'to_code','title'=>'税号','type'=>'string','show'=>array('usertype'=>2)),
        array('name'=>'to_bank','title'=>'开户行','type'=>'string','show'=>array('usertype'=>2)),
        array('name'=>'to_bankcode','title'=>'银行账号','type'=>'string','show'=>array('usertype'=>2)),
        array('name'=>'address.name','title'=>'收票人名称','type'=>'string'),
        array('name'=>'address.mobile','title'=>'收票人电话','type'=>'string'),
        array('name'=>'addressinfo','title'=>'收票人地址','type'=>'string'),
        array('name'=>'to_email','title'=>'收票人邮箱','type'=>'string'),
        array('name'=>'type','title'=>'发票类型','type'=>'radio','options'=>'type'),
        array('name'=>'sendtype','title'=>'领票方式','type'=>'radio','options'=>'sendtype'),
        array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
        array('name'=>'createtime','title'=>'时间','type'=>'time'),
    );
    $return['htmls']=$htmls;
    $return['item']=$item;
    exi($return);
}
if($_RQ['op']=='listday'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$y=$_RQ['y']?:date('Y');
	$m=$_RQ['m']?:date('m');
	if(!empty($_RQ['y'])){
		$return['params']['y']=$_RQ['y'];
	}
	if(!empty($_RQ['m'])){
		$return['params']['m']=$_RQ['m'];
	}
	$daynum=date("t",strtotime($y.'-'.$m));
	$list=array();
	for($i=1;$i<=$daynum;$i++){
		$starttime=strtotime($y.'-'.$m.'-'.$i);
		$endtime=$starttime+24*3600;
		$params['createtime between']=array($starttime,$endtime);
		$piao_fee=pdo_getsum('jy_aihire_fapiao',$params,'piao_fee');
		$list[]=array(
			'title'=>$y.'-'.$m.'-'.$i,
			'piao_fee'=>$piao_fee,
		);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'title','title'=>'日期','type'=>'string'),
			array('name'=>'piao_fee','title'=>'开票金额','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'y','width'=>15,'title'=>'年份','type'=>'radio','options'=>'ys'),
			array('name'=>'m','width'=>15,'title'=>'月份','type'=>'radio','options'=>'months'),
		)
	);
	$return['htmls']=$htmls;
	$nowy=date('Y');
	$y1=$nowy-2;
	$y2=$nowy-1;
	$return['ys']=array(
		$y1=>$y1.'年',
		$y2=>$y2.'年',
		$nowy=>$nowy.'年',
	);
	$return['months']=array(
		'1'=>'1月',
		'2'=>'2月',
		'3'=>'3月',
		'4'=>'4月',
		'5'=>'5月',
		'6'=>'6月',
		'7'=>'7月',
		'8'=>'8月',
		'9'=>'9月',
		'10'=>'10月',
		'11'=>'11月',
		'12'=>'12月',
	);
	exi($return);
}
if($_RQ['op']=='listmonth'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$y=$_RQ['y']?:date('Y');
	if(!empty($_RQ['y'])){
		$return['params']['y']=$_RQ['y'];
	}
	$list=array();
	for($i=1;$i<=12;$i++){
		$starttime=strtotime($y.'-'.$i);
		if($i==12){
			$ny=$y+1;
			$endtime=strtotime($ny.'-01');
		}else{
            $m=$i+1;
			$endtime=strtotime($y.'-'.$m);
		}
		$params['createtime between']=array($starttime,$endtime);
		$piao_fee=pdo_getsum('jy_aihire_fapiao',$params,'piao_fee');
		$list[]=array(
			'title'=>$y.'-'.$i,
			'piao_fee'=>$piao_fee,
		);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'title','title'=>'月份','type'=>'string'),
			array('name'=>'piao_fee','title'=>'开票金额','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'y','width'=>15,'title'=>'年份','type'=>'radio','options'=>'ys'),
		)
	);
	$return['htmls']=$htmls;
	$nowy=date('Y');
	$y1=$nowy-2;
	$y2=$nowy-1;
	$return['ys']=array(
		$y1=>$y1.'年',
		$y2=>$y2.'年',
		$nowy=>$nowy.'年',
	);
	exi($return);
}